package com.hbvtc.xxxy.nikeSale.frames;
/*
 * 添加商品信息
 */
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.hbvtc.xxxy.nikeSale.daos.GoodsDao;
import com.hbvtc.xxxy.nikeSale.models.GoodsShop;

public class AddShopNike extends JFrame{
	JButton btnAdd;
	JLabel lalId,lalNameJLabel,LalPrice,lalNum;
	JTextField txtId,txtName,txtPrice,txtNum;
	
	GoodsShop goodsShop=new GoodsShop();
	GoodsDao goodsDao=new GoodsDao();
	public AddShopNike(){
			this.setTitle("添加商品界面");			
			this.setVisible(true);
			this.setSize(1000, 700);
			this.setLocationRelativeTo(null);
			
			// 设置标题栏的图标
			ImageIcon imageIcon = new ImageIcon(getClass().getResource("01.png"));
			this.setIconImage(imageIcon.getImage());
			// 背景图片的路径
			String path = "images/001.jpg";
			ImageIcon background = new ImageIcon(path);
			// 把背景图片显示在一个标签里面
			JLabel label = new JLabel(background);
			// 把标签的大小位置设置为图片刚好填充整个面板
			label.setBounds(0, 0, this.getWidth(), this.getHeight());
			// 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
			JPanel imagePanel = (JPanel) this.getContentPane();
			imagePanel.setOpaque(false);
			// 把背景图片添加到分层窗格的最底层作为背景
			this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
	
			this.setLayout(null);
			JLabel lalId=new JLabel("商品编号：");
			JLabel lalName=new JLabel("商品名称：");
			JLabel lalPrice=new JLabel("商品价格：");
			JLabel lalNum=new JLabel("商品数量：");
			JButton btnAdd=new JButton("立即添加");
			final JTextField txtId=new JTextField();
			final JTextField txtName=new JTextField();
			final JTextField txtPrice=new JTextField();
			final JTextField txtNum=new JTextField();
			
			this.add(lalId);
			this.add(lalName);
			this.add(lalPrice);
			this.add(lalNum);
			this.add(btnAdd);
			this.add(txtId);
			this.add(txtName);
			this.add(txtPrice);
			this.add(txtNum);
			
			lalId.setBounds(150, 100, 120, 30);
			lalId.setFont(new Font(null, 0, 24));
			lalName.setBounds(150, 200, 120, 30);
			lalName.setFont(new Font(null, 0, 24));
			lalPrice.setBounds(150, 300, 120, 30);
			lalPrice.setFont(new Font(null, 0, 24));
			lalNum.setBounds(150, 400, 120, 30);
			lalNum.setFont(new Font(null, 0, 24));
			txtId.setBounds(400, 100, 250, 30);
			txtId.setFont(new Font(null, 0, 24));
			txtName.setBounds(400, 200, 250, 30);
			txtName.setFont(new Font(null, 0, 24));
			txtPrice.setBounds(400, 300, 250, 30);
			txtPrice.setFont(new Font(null, 0, 24));
			txtNum.setBounds(400, 400, 250, 30);
			txtNum.setFont(new Font(null, 0, 24));
			btnAdd.setBounds(580, 550, 150, 40);
			btnAdd.setFont(new Font(null, 0, 24));
			
			btnAdd.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					int id=Integer.parseInt(txtId.getText().trim());
					String name=txtName.getText();
					double price=Integer.parseInt(txtPrice.getText().trim());
					int num=Integer.parseInt(txtNum.getText().trim());
					try {
						goodsShop.setGoodsID(id);
						goodsShop.setGoodsName(name);
						goodsShop.setSalePrice(price);
						goodsShop.setStockNum(num);
						goodsDao=new GoodsDao();
						try {
							if(goodsDao.getByGoodsId(id)==true){
								goodsDao.AddGoods(goodsShop);
								JOptionPane.showMessageDialog(AddShopNike.this, "添加商品成功");
						}else {
								JOptionPane.showMessageDialog(AddShopNike.this, "商品编号重复");
							}
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						
					} catch (NumberFormatException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					} 
					
				}
			});

	}
	public static void main(String[] args) {
		new AddShopNike().setVisible(true);
	}
}
